package com.anywhere.freightman.car.service.impl;

import com.anywhere.freightman.car.dao.DriverDao;
import com.anywhere.freightman.car.model.CarModel;
import com.anywhere.freightman.car.model.DriverModel;
import com.anywhere.freightman.car.service.ICarService;
import com.anywhere.freightman.car.service.IDriverService;
import com.anywhere.freightman.common.BaseEntityService;
import com.anywhere.freightman.common.BaseMapper;
import com.anywhere.freightman.common.entity.car.DriverEntity;
import com.anywhere.freightman.common.entity.user.DriverQueryEntity;
import com.anywhere.freightman.common.enumeration.DriverStatus;
import com.anywhere.freightman.common.enumeration.TokenType;
import com.anywhere.freightman.common.ex.CustomException;
import com.anywhere.freightman.common.utils.BPwdEncoderUtils;
import com.anywhere.freightman.system.model.AccessToken;
import com.anywhere.freightman.system.service.IAccessTokenService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.weekend.Weekend;
import tk.mybatis.mapper.weekend.WeekendCriteria;

import java.util.List;

@Service
@Transactional
public class DriverService extends BaseEntityService<DriverModel, DriverQueryEntity> implements IDriverService {

    @Autowired
    private DriverDao driverDao;
    @Autowired
    private IAccessTokenService accessTokenService;
    @Autowired
    private ICarService carService;

    @Override
    public BaseMapper<DriverModel> getBaseDao() {
        return driverDao;
    }

    @Override
    public Example getExample(DriverQueryEntity entity) {
        Weekend<DriverModel> weekend = Weekend.of(DriverModel.class);
        WeekendCriteria<DriverModel, Object> criteria = weekend.weekendCriteria();

        if (entity == null) {
            return weekend;
        }

        if (!StringUtils.isEmpty(entity.getRealName())) {
            criteria.andLike("realName", "%" + entity.getRealName() + "%");
        }

        if (entity.getCompanyId() != null && entity.getCompanyId() > 0) {
            criteria.andEqualTo("companyId", entity.getCompanyId());
        }

        if (!StringUtils.isEmpty(entity.getIdentityNo())) {
            criteria.andLike("identityNo", "%" + entity.getIdentityNo() + "%");
        }

        return weekend;
    }

    @Override
    public void insert(DriverModel model) {

        if (selectByIdentityNo(model.getIdentityNo()) != null) {
            throw new CustomException("身份证号码重复");
        }

        if (selectByTel(model.getTel()) != null) {
            throw new CustomException("电话号码重复");
        }

        super.insert(model);
    }

    @Override
    public void delete(DriverModel model) {
        if (model.getStatus().equals(DriverStatus.WORKING)) {
            throw new CustomException("该司机在工作状态，无法删除");
        }
        super.delete(model);
    }

    @Override
    public DriverModel selectByIdentityNo(String identityNo) {
        return driverDao.selectByIdentityNo(identityNo);
    }

    @Override
    public DriverModel selectByTel(String tel) {
        return driverDao.selectByTel(tel);
    }

    @Override
    public List<DriverModel> getListByCompanyId(Long companyId) {
        return driverDao.selectListByCompanyId(companyId);
    }

    @Override
    public DriverEntity login(String tel, String password) {
        DriverModel model = driverDao.selectByTel(tel);
        if (model == null || !BPwdEncoderUtils.matches(password, model.getPassword())) {
            throw new CustomException("账号或密码错误");
        }

        if (model.getStatus().equals(DriverStatus.DISABLE)) {
            throw new CustomException("账号被禁用，请联系管理员");
        }

        DriverEntity entity = new DriverEntity();
        BeanUtils.copyProperties(model, entity);
        AccessToken token = accessTokenService.insert(entity.getId(), TokenType.DRIVER);
        entity.setToken(token.getToken());
        return entity;
    }

    @Override
    public DriverModel selectByWxAppId(String wxAppId) {
        return driverDao.selectByWxAppId(wxAppId);
    }

    @Override
    public List<CarModel> getCarList(Long driverId) {
        return driverDao.getCarList(driverId);
    }

    @Override
    public void remove(long id) {
        DriverModel model = getById(id);
        if (model == null) {
            throw new CustomException("司机信息不存在");
        }
        carService.removeDriverByDriver(model.getId());
        delete(model);
    }

    @Override
    public void cleanWxOpenId(String openId) {
        driverDao.deleteWxOpenId(openId);
    }

    @Override
    public void changeStatus(long driverId, DriverStatus status) {
        driverDao.changeStatus(driverId, status);
    }

    @Override
    public void updatePwd(long driverId, String oldPwd, String newPwd) {
        DriverModel model = getById(driverId);
        if (model == null) {
            throw new CustomException("用户不存在");
        }
        if (!BPwdEncoderUtils.matches(oldPwd, model.getPassword())) {
            throw new CustomException("密码错误");
        }
        String password = BPwdEncoderUtils.BCryptPassword(newPwd);
        driverDao.updatePwd(model.getId(),password);
    }
}
